/*
* BSTimelineModule
*
* Ken Frederick
* frederickk@google.com
*
*/

var BSTimelineData = function(data) {
    // ------------------------------------------------------------------------
    //
    // Properties
    //
    // ------------------------------------------------------------------------
    // instantiate a base to share common
    // properties and methods across BSTimeline classes
    // (bootleg inheritance :)
    var base = BSTimelineBase;

    data = base.combine(
        {},
        data
    );

    var columns = {};
    var columnTitles = [];

    var rows = {};
    var rowTitles = [];

    var dates = [];



    // ------------------------------------------------------------------------
    //
    // Methods
    //
    // ------------------------------------------------------------------------
    (function init() {
        // Clean up Data
        // push the column titles into an array
        for (var val in data[0]) {
            if (val != 'rowNumber') {
                columnTitles.push(data[0][val].trimEnd());
            }
        }

        var i = 0;
        for (var d in data) {
            var entry = data[d];
            var j = 0; // the 0 key is rowNumber

            var cti = 0;
            for (var val in entry) {
                if (val != 'rowNumber') {
                    var key = String(columnTitles[cti]).toLowerCase();
                    // replace generic column names
                    // with specific column header titles
                    entry[key] = entry[val];

                    // get rid of nonsense title
                    delete entry[val];

                    // add new array to rows
                    if (i === 0) {
                        // console.log('entry[key]\t' + entry[key]);

                        rowTitles.push(entry[key]);
                        rows[entry[key]] = {
                            'colNumber': cti
                        };

                        if (!isNaN(Date.parse(entry[key]))) {
                            dates.push(new Date(entry[key]));
                        }

                    }

                    cti++;
                } // end if != rowNumber

            } // end for entry


            var index = 0;
            for (var val in entry) {
                if (val != 'rowNumber') {
                    for (var n = 0; n < rowTitles.length; n++) {
                        var key = Object.keys(entry)[n+1];
                        // console.log(rowTitles[n] + '\t---\t' + entry[key]);

                        rows[rowTitles[n]][String(i)] = entry[key];
                    }
                }

                index++;
            }

            i++;
        }

        // add date field
        // Array of all dates
        dates = dates.sort(function(a, b) { return b-a });
        data = base.combine(
            {
                'dates'      : dates,
                'date-first' : dates[0],
                'date-last'  : dates[dates.length-1],
                'date-range' : [dates[0], dates[dates.length-1]]
            },
            data
        );


        // console.log('------------------');

        // console.log('rows');
        // console.log(rowTitles);
        // console.log(rows);

        // console.log('------------------');

        // console.log('columns');
        // console.log(columnTitles);
        // console.log(data);
    })();


    // ------------------------------------------------------------------------
    var getData = function() {
        return data;
    };

    var getColumns = function() {
        return columns;
    };
    var getRows = function() {
        return rows;
    };

    var getDates = function() {
        return dates;
    };


    // ------------------------------------------------------------------------
    return {
        data    : getData,
        columns : getColumns,
        rows    : getRows,
        dates   : getDates
    };

};
